import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


def gen_series_test():
    s = pd.Series([1, 3, 5, np.nan, 6, 8])
    print(s)
    print("to_json:", s.to_json())
    dates = pd.date_range('20130101', periods=6)
    print(dates)

def gen_dataframe_test():
    dates = pd.date_range('20130101', periods=6)
    df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
    print(df)
    # iter df
    for index, row in df.iterrows():
        print(index, row.values)

    str = df.to_json(orient='index')
    print("to_json:", str)

def dataframe_change_test():
    dates = pd.date_range('20130101', periods=3)
    a = np.arange(12).reshape(3, 4)
    df = pd.DataFrame(a, index=dates, columns=list('ABCD'))
    print("---"*30)
    print(df)
    df.iloc[1, :] = np.nan
    print("after change:")
    print(df)

    df = df.fillna(0)
    results = []
    for index, row in df.T.iteritems():
        v = row.values.tolist()
        v.insert(0, index.strftime('%Y-%m-%d'))
        print(v)

    for index, row in df.iterrows():
        v = row.values.tolist()
        v.insert(0, index.strftime('%Y-%m-%d'))
        print(v)


if __name__ == '__main__':
    gen_series_test()
    gen_dataframe_test()
    dataframe_change_test()

